package com.ruoyi.system.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TicketMovieShowTime;
import com.ruoyi.system.service.ITicketMovieShowTimeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TicketTemplateMovieShowTime;
import com.ruoyi.system.service.ITicketTemplateMovieShowTimeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 电影放映时间模板，具体放映时间从模板生成Controller
 * 
 * @author ruoyi
 * @date 2022-01-10
 */
@Controller
@RequestMapping("/system/tickettemplatemovieshowtime")
public class TicketTemplateMovieShowTimeController extends BaseController
{
    private String prefix = "system/tickettemplatemovieshowtime";

    @Autowired
    private ITicketTemplateMovieShowTimeService ticketTemplateMovieShowTimeService;


    @Autowired
    private ITicketMovieShowTimeService ticketMovieShowTimeService;

    @RequiresPermissions("system:tickettemplatemovieshowtime:view")
    @GetMapping()
    public String tickettemplatemovieshowtime()
    {
        return prefix + "/tickettemplatemovieshowtime";
    }

    /**
     * 查询电影放映时间模板，具体放映时间从模板生成列表
     */
    @RequiresPermissions("system:tickettemplatemovieshowtime:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TicketTemplateMovieShowTime ticketTemplateMovieShowTime)
    {
        startPage();
        List<TicketTemplateMovieShowTime> list = ticketTemplateMovieShowTimeService.selectTicketTemplateMovieShowTimeList(ticketTemplateMovieShowTime);
        return getDataTable(list);
    }

    /**
     * 导出电影放映时间模板，具体放映时间从模板生成列表
     */
    @RequiresPermissions("system:tickettemplatemovieshowtime:export")
    @Log(title = "电影放映时间模板，具体放映时间从模板生成", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TicketTemplateMovieShowTime ticketTemplateMovieShowTime)
    {
        List<TicketTemplateMovieShowTime> list = ticketTemplateMovieShowTimeService.selectTicketTemplateMovieShowTimeList(ticketTemplateMovieShowTime);
        ExcelUtil<TicketTemplateMovieShowTime> util = new ExcelUtil<TicketTemplateMovieShowTime>(TicketTemplateMovieShowTime.class);
        return util.exportExcel(list, "电影放映时间模板，具体放映时间从模板生成数据");
    }

    /**
     * 新增电影放映时间模板，具体放映时间从模板生成
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存电影放映时间模板，具体放映时间从模板生成
     */
    @RequiresPermissions("system:tickettemplatemovieshowtime:add")
    @Log(title = "电影放映时间模板，具体放映时间从模板生成", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TicketTemplateMovieShowTime ticketTemplateMovieShowTime)
    {
        logger.debug("收到电影模板添加="+ticketTemplateMovieShowTime);
        return toAjax(ticketTemplateMovieShowTimeService.insertTicketTemplateMovieShowTime(ticketTemplateMovieShowTime));
    }

    /**
     * 修改电影放映时间模板，具体放映时间从模板生成
     */
    @RequiresPermissions("system:tickettemplatemovieshowtime:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TicketTemplateMovieShowTime ticketTemplateMovieShowTime = ticketTemplateMovieShowTimeService.selectTicketTemplateMovieShowTimeById(id);
        mmap.put("ticketTemplateMovieShowTime", ticketTemplateMovieShowTime);
        return prefix + "/edit";
    }

    /**
     * 修改保存电影放映时间模板，具体放映时间从模板生成
     */
    @RequiresPermissions("system:tickettemplatemovieshowtime:edit")
    @Log(title = "电影放映时间模板，具体放映时间从模板生成", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TicketTemplateMovieShowTime ticketTemplateMovieShowTime)
    {
        return toAjax(ticketTemplateMovieShowTimeService.updateTicketTemplateMovieShowTime(ticketTemplateMovieShowTime));
    }

    /**
     * 删除电影放映时间模板，具体放映时间从模板生成
     */
    @RequiresPermissions("system:tickettemplatemovieshowtime:remove")
    @Log(title = "电影放映时间模板，具体放映时间从模板生成", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ticketTemplateMovieShowTimeService.deleteTicketTemplateMovieShowTimeByIds(ids));
    }

    /**
     * 生成具体放映时间
     */
//    @RequiresPermissions("system:tickettemplatemovieshowtime:edit")
    @PostMapping("/generateShowTime/{id}")
    @ResponseBody
    public AjaxResult generateShowTime(@PathVariable("id") Long id)//, ModelMap mmap)
    {
        TicketTemplateMovieShowTime ticketTemplateMovieShowTime = ticketTemplateMovieShowTimeService.selectTicketTemplateMovieShowTimeById(id);
    //    mmap.put("ticketTemplateMovieShowTime", ticketTemplateMovieShowTime);
 logger.debug("生成放映时间，电影id="+id+",电影模板信息="+ticketTemplateMovieShowTime);


        TicketMovieShowTime ticketMovieShowTimeTemp = new TicketMovieShowTime();
        ticketMovieShowTimeTemp.setMovieId(ticketTemplateMovieShowTime.getMovieId());
        //查找该电影的所有放映数据
        List<TicketMovieShowTime> list = this.ticketMovieShowTimeService.selectTicketMovieShowTimeList(ticketMovieShowTimeTemp);
        if(list.size() > 0)
        {
            return  AjaxResult.error("放映时间已经生成过！需要先删除再重新生成");
        }



        int days =   DateUtils.differentDaysByMillisecond(ticketTemplateMovieShowTime.getPlayStartDate(), ticketTemplateMovieShowTime.getPlayEndDate());
        logger.debug("日期天数="+days+",开始日期="+ticketTemplateMovieShowTime.getPlayStartDate()+",结束日期="+ticketTemplateMovieShowTime.getPlayEndDate());

        //遍历日期
        Long startTime = ticketTemplateMovieShowTime.getPlayStartDate().getTime();// kqKqsjzl.getKsrq().getTime();
        Long endTime = ticketTemplateMovieShowTime.getPlayEndDate().getTime();// kqKqsjzl.getJsrq().getTime();
        Long oneDay = 1000 * 60 * 60 * 24l;

        Long time = startTime;
        while (time <= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println("遍历日期="+df.format(d)+",time="+time+",endTime="+endTime+",oneDay="+oneDay);


            TicketMovieShowTime ticketMovieShowTime = new TicketMovieShowTime();
            ticketMovieShowTime.setShowNumber(ticketTemplateMovieShowTime.getShowNumber());
            ticketMovieShowTime.setShowName(ticketTemplateMovieShowTime.getShowName());
            ticketMovieShowTime.setTicketCount(ticketTemplateMovieShowTime.getTicketCount());
            ticketMovieShowTime.setSoldCount(0);//ticketTemplateMovieShowTime.getShowNumber());
            ticketMovieShowTime.setLeftCount(ticketTemplateMovieShowTime.getTicketCount());//.getShowNumber());
            //具体日期
            //     ticketMovieShowTime.setPlayDate(ticketTemplateMovieShowTime.getPlayStartDate());
            ticketMovieShowTime.setPlayTime(ticketTemplateMovieShowTime.getPlayTime());
            ticketMovieShowTime.setMovieId(ticketTemplateMovieShowTime.getMovieId());
            ticketMovieShowTime.setMovieName(ticketTemplateMovieShowTime.getMovieName());
            ticketMovieShowTime.setCreateTime(DateUtils.getNowDate());

            ticketMovieShowTime.setPlayDate(d);
            //插入数据
            logger.debug("准备插入数据放映日期="+ticketMovieShowTime.getPlayDate());

            this.ticketMovieShowTimeService.insertTicketMovieShowTime(ticketMovieShowTime);
            time += oneDay;
        }


 //       return tickettemplatemovieshowtime();
//        return prefix ;//+ "/generateShowTime";
        return AjaxResult.success("操作成功");
    }


    /**
     * 删除具体放映时间
     */
//    @RequiresPermissions("system:tickettemplatemovieshowtime:edit")
    @PostMapping("/deleteShowTime/{id}")
    @ResponseBody
    public AjaxResult deleteShowTime(@PathVariable("id") Long id)//, ModelMap mmap)
    {
        TicketTemplateMovieShowTime ticketTemplateMovieShowTime = ticketTemplateMovieShowTimeService.selectTicketTemplateMovieShowTimeById(id);
        //    mmap.put("ticketTemplateMovieShowTime", ticketTemplateMovieShowTime);
        logger.debug("删除放映时间，电影id="+id+",电影模板信息="+ticketTemplateMovieShowTime);

        //从具体的放映时间查找并删除
        //查询满足条件的数据
        TicketMovieShowTime ticketMovieShowTime = new TicketMovieShowTime();
        ticketMovieShowTime.setMovieId(ticketTemplateMovieShowTime.getMovieId());
        //查找该电影的所有放映数据
        List<TicketMovieShowTime> list = this.ticketMovieShowTimeService.selectTicketMovieShowTimeList(ticketMovieShowTime);
        for (TicketMovieShowTime item : list         ) {
            logger.debug("找到的数据="+item);
            //逐个删除,，如果已经在售的电影不能删除
            if(item.getLeftCount() < item.getTicketCount())
            {
                //跳过正在售票的场次,需要给出反馈，已经出售的场次
                // continue;
                return AjaxResult.success("操作中断，已经有售票数据，场次【"+item.getId()+",时间:"+item.getPlayDate()+"】不可删除");
            }

            this.ticketMovieShowTimeService.deleteTicketMovieShowTimeById(item.getId());
        }
//
        return AjaxResult.success("操作成功");
//        return tickettemplatemovieshowtime();// + "/deleteShowTime";
    }

}
